﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace A2DFramework.EventService.Configuration
{
    public static class EventServiceConfiguration
    {
        public static RedisSettings RedisSettings;
        public static SuperSocketSettings SuperSocketSettings;
        public static LocalSettings LocalSettings;

        public static List<EventMapping> EventMappings;

        public static EventBusType EventBusType;


        static EventServiceConfiguration()
        {
            string configPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config");
            if (!File.Exists(configPath))
                throw new Exception("Cannot find eventbus config file.");

            XElement root = XElement.Load(configPath);

            root = root.Element("EventSetting");
            if (root == null)
                throw new Exception("EventSetting node not exists");

            if (root.Attribute("EventBusType").Value.Equals("SuperSocket", StringComparison.OrdinalIgnoreCase))
            {
                root = root.Element("SuperSocket");

                EventBusType = Configuration.EventBusType.SuperSocket;

                SuperSocketSettings setting = new SuperSocketSettings();

                setting.Address = root.Element("Address").Value;
                setting.Identifier = root.Element("Identifier").Value;
                setting.Port = int.Parse(root.Element("Port").Value);
                setting.RetryConnectInterval = double.Parse(root.Element("RetryConnectInterval").Value);

                SuperSocketSettings = setting;
            }
            else if (root.Attribute("EventBusType").Value.Equals("Redis", StringComparison.OrdinalIgnoreCase))
            {
                root = root.Element("Redis");

                EventBusType = Configuration.EventBusType.Redis;

                RedisSettings setting = new RedisSettings();

                setting.ReadWriteServerIps = new List<string>();
                setting.ReadOnlyServerIps = new List<string>();

                {
                    string[] serverIps = root.Element("ReadWriteServerIps").Value.Trim().Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (serverIps != null)
                        setting.ReadWriteServerIps = serverIps.ToList();
                }
                {
                    string[] serverIps = root.Element("ReadOnlyServerIps").Value.Trim().Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (serverIps != null)
                        setting.ReadOnlyServerIps = serverIps.ToList();
                }

                setting.MaxReadPoolSize = int.Parse(root.Element("MaxReadPoolSize").Value);
                setting.MaxWritePoolSize = int.Parse(root.Element("MaxWritePoolSize").Value);
                setting.Identifier = root.Element("Identifier").Value;

                RedisSettings = setting;
            }
            else if (root.Attribute("EventBusType").Value.Equals("Local", StringComparison.OrdinalIgnoreCase))
            {
                root = root.Element("Local");

                EventBusType = Configuration.EventBusType.Local;

                LocalSettings setting = new LocalSettings();

                LocalSettings = setting;
            }
            else
            {
                throw new Exception("Invalid EventBusType");
            }


            root = XElement.Load(configPath);
            root = root.Element("EventSetting");
            root = root.Element("EventMapping");

            List<EventMapping> mapping=new List<EventMapping>();
            foreach (var xmlRow in root.Elements("Event"))
            {
                string evt = xmlRow.Attribute("Event").Value;
                string evtFullType = xmlRow.Attribute("EventFullType").Value;
                string evtProcessor = xmlRow.Value;

                mapping.Add(new EventMapping() {  Event=evt, EventFullType=evtFullType, EventProcessorType=evtProcessor});
            }
            EventMappings = mapping;
        }
    }
}
